// src/stores/materialOutbound.ts
import { defineStore } from 'pinia';
import axios from '@/plugins/axios';

export interface MaterialOutbound {
  id: number;
  outboundNumber: string;
  materialId: number | null;
  materialName: string;
  quantity: number;
  status: string;
  operatorId: number | null;
  operatorName: string;
  reviewerId: number | null;
  outboundTime: string;
  createTime?: string;
}

export const outboundFields = [
  {
    key: 'outboundNumber',
    label: '出库编号',
    type: 'text',
    placeholder: '请输入出库编号（如 OUT001）',
    maxLength: 10,
  },
  {
    key: 'materialId',
    label: '物资',
    type: 'select',
    placeholder: '请选择物资',
    options: [] as { value: number; label: string }[],
  },
  {
    key: 'quantity',
    label: '数量',
    type: 'number',
    placeholder: '请输入数量',
  },
  {
    key: 'operatorId',
    label: '操作人',
    type: 'select',
    placeholder: '请选择操作人',
    options: [] as { value: number; label: string }[],
  },
];

export const useMaterialOutboundStore = defineStore('materialOutbound', {
  state: () => ({
    outbounds: [] as MaterialOutbound[],
  }),
  actions: {
    async fetchOutbounds() {
      try {
        const response = await axios.get('/material-outbounds');
        if (response.data.code === 200) {
          this.outbounds = response.data.data;
          return this.outbounds;
        }
        throw new Error(response.data.message || '获取出库记录失败');
      } catch (error) {
        throw error;
      }
    },
    async createOutbound(data: { outboundNumber: string; materialId: number; quantity: number; operatorId: number }) {
      try {
        const response = await axios.post('/material-outbounds', data);
        if (response.data.code === 200) {
          return response.data.data;
        }
        throw new Error(response.data.message || '创建出库记录失败');
      } catch (error) {
        throw error;
      }
    },
    async updateOutbound(data: MaterialOutbound) {
      try {
        const response = await axios.put('/material-outbounds', {
          id: data.id,
          outboundNumber: data.outboundNumber,
          materialId: data.materialId,
          quantity: data.quantity,
          operatorId: data.operatorId,
        });
        if (response.data.code === 200) {
          return response.data.data;
        }
        throw new Error(response.data.message || '更新出库记录失败');
      } catch (error) {
        throw error;
      }
    },
    async deleteOutbound(id: number) {
      try {
        const response = await axios.delete(`/material-outbounds/${id}`);
        if (response.data.code === 200) {
          return response.data.data;
        }
        throw new Error(response.data.message || '删除出库记录失败');
      } catch (error) {
        throw error;
      }
    },
    async reviewOutbound(data: { id: number; status: string; reviewerId: number }) {
      try {
        const response = await axios.put('/material-outbounds/review', data);
        if (response.data.code === 200) {
          return response.data.data;
        }
        throw new Error(response.data.message || '审核出库记录失败');
      } catch (error) {
        throw error;
      }
    },
  },
});